from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, JsonResponse
from . import models
from python_sdk import bcos


# Create your views here.
def login(request):
    # # 强转request的数据类型
    # loginRequest = HttpRequest(request)
    # 从request中获取参数信息
    paramMap = request.GET
    username = paramMap.get("username")
    password = paramMap.get("password")
    if username is None or len(username) == 0:
        return JsonResponse({"code": -100, "msg": "没有填写用户名"})
    if password is None or len(password) == 0:
        return JsonResponse({"code": -100, "msg": "没有填写密码"})
    # 获取用户名
    if paramMap.get("username") is not None:
        # 使用用户传递的name查询数据库
        result = bcos.login(name=username, password=password)
        print(result)
        if result is None or len(result) == 0 or result[0] == False:
            return JsonResponse({"code": -1, "msg": "登录失败"})
        else:
            # 登录成功后，将当前用户的账号和密码缓存在session中
            request.session['username'] = username
            request.session['password'] = password
            return JsonResponse({"code": 0, "msg": "登录成功"})
    else:
        return JsonResponse({"code": -100, "msg": "没有填写用户名"})


# 注册接口
def register(request):
    paramMap = request.GET
    userName = paramMap.get('username')
    passWord = paramMap.get('password')
    name = paramMap.get('name')
    if userName is None or len(userName) == 0:
        return JsonResponse({"code": -1, "msg": "用户名不能为空！"})
    if passWord is None or len(passWord) == 0:
        return JsonResponse({"code": -1, "msg": "密码不能为空！"})
    result = bcos.register(userName, passWord)
    print(result)
    if result is not None and int(result["status"], 0) == 0:
        return JsonResponse({"code": 0, "msg": "注册成功"})
    else:
        return JsonResponse({"code": -1, "msg": result["statusMsg"]})


# 任务列表
def tasklist(request):
    session = _checkLogin(request)
    if session is None:
        return JsonResponse({"code": -1, "msg": "未登录，请重新登录"})
    result = bcos.queryAll()
    return JsonResponse({"code": 0, "msg": "任务列表获取成功", "data": {"data": result}})


def issue(request):
    paramMap = request.GET
    task_name = paramMap.get("task_name")
    bonus = paramMap.get("bonus")
    session = _checkLogin(request)
    if session is None:
        return JsonResponse({"code": -1, "msg": "未登录，请重新登录"})
    username = session[0]
    password = session[1]
    result = bcos.issue(username, password, task_name, int(bonus))
    print(result)
    if result is not None and int(result["status"], 0) == 0:
        return JsonResponse({"code": 0, "msg": "任务发布成功"})
    else:
        return JsonResponse({"code": -1, "msg": result["statusMsg"]})


def update(request):
    paramMap = request.GET
    task_id = int(paramMap.get("task_id"))
    task_status = int(paramMap.get("task_status"))
    comment = paramMap.get("comment")
    session = _checkLogin(request)
    if session is None:
        return JsonResponse({"code": -1, "msg": "未登录，请重新登录"})
    username = session[0]
    password = session[1]
    if task_status == 1: # 接受任务
        result = bcos.take(username, password, task_id)
    elif task_status == 2: # 提交任务
        result = bcos.commit(username, password, task_id)
    elif task_status == 3: # 确认任务
        result = bcos.confirm(username, password, task_id, comment, task_status)
    elif task_status == 4: # 退回任务
        result = bcos.confirm(username, password, task_id, comment, task_status)
    print(result)
    if result is not None and int(result["status"], 0) == 0:
        return JsonResponse({"code": 0, "msg": "任务更新成功"})
    else:
        return JsonResponse({"code": -1, "msg": result["statusMsg"]})


def balance(request):
    paramMap = request.GET
    username = paramMap.get("username")
    session = _checkLogin(request)
    if session is None:
        return JsonResponse({"code": -1, "msg": "未登录，请重新登录"})
    if username != session[0]:
        return JsonResponse({"code": -1, "msg": "当前登录账号与缓存账号不一致，请重新登录"})
    result = bcos.balanceOf(username)
    print(result)
    if result is not None:
        return JsonResponse({"code": 0, "msg": "获取账号成功", "data": {"data": {"amount": result[0]}}})
    else:
        return JsonResponse({"code": -1, "msg": result["statusMsg"]})


def logout(request):
    request.session.clear()
    return JsonResponse({"code": 0, "msg": "注销成功"})


# 检查是否已登录
# 如果session中没有用户名和密码，返回None，如果已登录，则返回用户名和密码
def _checkLogin(request):
    if request.session.get("username") is None or request.session.get("password") is None:
        return None
    return request.session.get("username"), request.session.get("password")
